﻿Imports System.Data.OleDb


Public Class addNewTopic
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Submit.Click

        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_RPicture.FileName)
        If fup_RPicture.HasFile Then
            fup_RPicture.SaveAs(Server.MapPath("Images") & "/" & newFileName)
        End If


        Dim sql As String = "INSERT INTO TBL_Topics (T_Title, T_Text, T_Picture, T_Writer, T_time, T_type) VALUES (@f1,@f2,@f3,@f4,Now(),@f6)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", TB_Title.Text)
        cmd.Parameters.AddWithValue("@f2", TB_Text.Text)
        cmd.Parameters.AddWithValue("@f3", newFileName)
        cmd.Parameters.AddWithValue("@f4", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f6", DDL_Type.SelectedValue)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        ChangeUserTitle()

        Response.Redirect("list.aspx?Type=" & DDL_Type.SelectedValue)

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Sub ChangeUserTitle()


        Dim SqlString As String
        Dim CommentCount As Long
        Dim TopicsCount As Long
        Dim ParticipationCount As Long
        Dim title As String

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        SqlString = "SELECT TopicsCount, CommentsCount FROM CountUserCommentTopicsSum where Writer = @1 "
        Dim ReadCmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        ReadCmd.CommandType = CommandType.Text
        oleDbConn.Open()
        ReadCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        Dim dr As OleDbDataReader = ReadCmd.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("TopicsCount")) Then TopicsCount = dr("TopicsCount") Else TopicsCount = 0
            If Not IsDBNull(dr("CommentsCount")) Then CommentCount = dr("CommentsCount") Else CommentCount = 0
        End If

        ParticipationCount = TopicsCount + CommentCount

        title = ""
        If ParticipationCount < 100 Then
            title = "New Member"
        ElseIf ParticipationCount >= 100 And ParticipationCount < 200 Then
            title = "Member"
        ElseIf ParticipationCount >= 200 And ParticipationCount < 300 Then
            title = "Active Member"
        ElseIf ParticipationCount >= 300 Then
            title = "Senior Member"
        End If

        SqlString = "Update TBL_Profile set UserTitle=@f1 where UserName = @f2"
        Dim UpdateCmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        UpdateCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        UpdateCmd.Parameters.AddWithValue("@f2", title)

        UpdateCmd.ExecuteNonQuery()
        oleDbConn.Close()
        UpdateCmd.Dispose()
        oleDbConn.Dispose()

    End Sub
End Class